package com.pangu.ui.view;

import com.pangu.AMApplication;
import com.pangu.ui.AActivity;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewParent;
import android.widget.LinearLayout;

import java.lang.ref.WeakReference;

/**
 * 普通视图抽象父类
 * <p/>
 * Created by cxlin
 */
public abstract class CommonInfoView<A extends AMApplication> extends LinearLayout {

    public static final String TAG = CommonInfoView.class.getSimpleName();

    protected A imContext;
    // 视图内部的消息处理器, 所有子类共享
    private ViewMessageHandler ViewMessageHandler;

    public CommonInfoView(Context context) {
        super(context);

        initCommonInfoView();
    }

    public CommonInfoView(Context context, AttributeSet attrs) {
        super(context, attrs);

        initCommonInfoView();
    }

    // 初始化视图
    private void initCommonInfoView() {
        setOrientation(LinearLayout.VERTICAL);

        imContext = (A) AMApplication.getInstance();
    }

    public void addView(int layoutResID) {
        // 内容布局
        LayoutInflater.from(getContext()).inflate(layoutResID, this);
    }

    /**
     * 必要时刷新视图, 用于显示给用户的时候调用
     *
     * @param what
     */
    public void flushView(int what) {

    }

    /**
     * 视图被移除，它的触发局限在其父亲组件内。
     */
    protected void onViewRemove() {
    }

    /**
     * 视图被添加，它的触发局限在其父亲组件内。
     */
    protected void onViewAttach() {
    }

    /**
     * 视图摧毁，它的触发局限在其父Activity组件内。
     */
    protected void onViewDestory() {

    }

    /**
     * 依次选择视图，一般用在用户按下回退按钮时候的行为。
     *
     * @return 是否已经 被处理
     */
    protected boolean rotateContentView() {
        return false;
    }

    /**
     * 根据传入的mark进行合理的处理视图
     *
     * @param message
     */
    public void handleChainMessage(Message message) {

    }

    /**
     * 给父亲(父亲必须是CommonInfoView类型)发送消息，按视图层次依次次派发消息。<br>
     * 最终消息由handleChainMessage(Message message)处理。
     *
     * @param msg
     */
    protected void notifyMessageToParent(Message msg) {
        ViewParent viewParent = getParent();
        while (viewParent instanceof View) {
            if (viewParent instanceof CommonInfoView) {
                CommonInfoView parent = (CommonInfoView) viewParent;
                // 这里只能调用handleChainMessage
                parent.handleChainMessage(msg);
            }

            viewParent = viewParent.getParent();
        }

        // 通知父activity
        if (getContext() instanceof AActivity) {
            ((AActivity) getContext()).subHandleMessage(msg);
        }
    }

    /**
     * 发送延时的消息
     *
     * @param message
     */
    protected void sendMessage(Message message) {
        if (ViewMessageHandler == null) {
            ViewMessageHandler = new ViewMessageHandler(this);
        }
        ViewMessageHandler.sendMessage(message);
    }

    protected void sendEmptyMessage(int what) {
        if (ViewMessageHandler == null) {
            ViewMessageHandler = new ViewMessageHandler(this);
        }
        ViewMessageHandler.sendEmptyMessage(what);
    }

    /**
     * 移除旧的消息
     *
     * @param what
     * @param delay
     */
    protected void sendEmptyMessageDelay(int what, long delay) {
        removeEmptyMessage(what);
        ViewMessageHandler.sendEmptyMessageDelayed(what, delay);
    }

    /**
     * 移除某个消息
     *
     * @param what
     */
    protected void removeEmptyMessage(int what) {
        if (ViewMessageHandler == null) {
            ViewMessageHandler = new ViewMessageHandler(this);
        }
        ViewMessageHandler.removeMessages(what);
    }

    /**
     * 视图内部的消息处理器
     */
    private static class ViewMessageHandler extends Handler {

        private final WeakReference<CommonInfoView> mCommonInfoViewRef;

        public ViewMessageHandler(CommonInfoView commonInfoView) {
            mCommonInfoViewRef = new WeakReference<CommonInfoView>(commonInfoView);
        }

        @Override
        public void handleMessage(Message msg) {
            CommonInfoView commonInfoView = mCommonInfoViewRef.get();
            if (commonInfoView != null) {
                commonInfoView.handleChainMessage(msg);
            }
        }

    }

}
